--[[
	Wiki Lookup
	commands: wiki_lookup <type> <args>
		ex: wiki_lookup library string sub
			- Looks up string.sub 
	chat: !lookup <type> args
		ex: !lookup library string sub
			- Looks up string.sub

	Author: Adult
]]--

-- The types of shit you can look up
local types = { 
	["class"]   = "Classes",
	["enum"]    = "Enums",
	["event"]   = "Events",
	["global"]  = "Globals",
	["hook"]    = "Hooks", 
	["library"] = "Libraries",
	["vgui"]    = "VGUI"
}

-- Opens the given url
local function open( url )
	local w, h = ScrW(), ScrH()
	local Frame = vgui.Create( "DFrame" )
		Frame:SetSize( w - 100, h - 100 )
		Frame:Center()
		Frame:SetTitle( "Garry's Mod Wiki" )
		Frame:MakePopup()

	local HTML = vgui.Create( "DHTML", Frame )
		HTML:SetSize( w - 110, h - 180 )
		HTML:SetPos( 5, 40 )
		HTML:OpenURL( url )

	local ctrl = vgui.Create( "DHTMLControls", Frame )
		ctrl:SetPos( 5, 30 )
		ctrl:SetSize( w - 125, 40 )
		ctrl:SetHTML( HTML )

end

-- the lookup function
local function lookup( args )
	local t = table.remove( args, 1 )

	if not types[t] then return end
	
	local url = "wiki.garrysmod.com/page/" .. types[t]

	for i=1,#args do 
		url = url .. "/" .. args[i]
	end

	open( url )
end

concommand.Add( "wiki_lookup", function( _, _, a ) lookup( a ) end )

-- This is so we don't have to keep going to console
local function playerChat( ply, text )
	if ply ~= LocalPlayer() then return end
	local args = string.Explode( " ", text )

	if args[1] ~= "!lookup" then return end 
	table.remove( args, 1 )
	lookup( args )

	return true 
end

hook.Add( "OnPlayerChat", "WikiLookup", playerChat )